package com.shiguo365.market.display.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.R;
import com.shiguo365.market.display.activity.OrderConfirmActivity;
import com.shiguo365.market.display.adapter.UserCartAdapter;
import com.shiguo365.market.display.util.RefreshUtils;
import com.shiguo365.market.display.view.ICartView;
import com.shiguo365.market.presenter.contract.ICartPresenter;
import com.shiguo365.market.presenter.implement.CartPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by caomenglong on 16/7/7.
 */
public class CartFragment extends Fragment implements ICartView,SwipeRefreshLayout.OnRefreshListener {

    private static final String TAG = "CartFragment";

    @BindView(R.id.cart_recycler_view)
    protected RecyclerView recyclerView;

    @BindView(R.id.cart_refresh_layout)
    protected SwipeRefreshLayout refreshLayout;

    @BindView(R.id.cart_icon_no_data)
    protected TextView iconNoData;

    @BindView(R.id.buttonCreateOrder)
    protected Button createOrderButton;

    private List<JSONObject> mObjects = new ArrayList<JSONObject>();

    private UserCartAdapter mAdapter;

    private ICartPresenter mPresenter;

    private Integer uid = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_cart, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this,view);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new UserCartAdapter(getActivity(), mObjects);
        recyclerView.setAdapter(mAdapter);

        mPresenter = new CartPresenter(getActivity(),this);

        RefreshUtils.initOnCreate(refreshLayout, this);
        RefreshUtils.refreshOnCreate(refreshLayout, this);


    }


    @OnClick(R.id.buttonCreateOrder)
    public void createOrder(){
        Log.e(TAG,"createOrder()");
        Intent intent=new Intent(this.getActivity(), OrderConfirmActivity.class);
        startActivity(intent);
    }

    @Override
    public void onRefresh() {
        Log.e(TAG,"onRefresh()");
        mPresenter.getCartById(uid);
    }


    @Override
    public boolean onGetCartOk(@NonNull List<JSONObject> result) {
        mObjects.clear();
        mObjects.addAll(result);
        mAdapter.notifyDataSetChanged();
        iconNoData.setVisibility(mObjects.size() == 0 ? View.VISIBLE : View.GONE);
        return true;
    }

    @Override
    public void onGetCartFinished() {
        refreshLayout.setRefreshing(false);
    }
}
